package com.czwj.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.Department;
import com.czwj.entities.DataGridData;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.UserInfo;
import com.czwj.service.DepartmentService;
import com.czwj.util.TextUtil;

@Controller
public class DepartmentAction{
	@Resource
	DepartmentService departmentService;
	
	@RequestMapping(value = "/department",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Department> department(@RequestParam(value = "companyid",required = false) Integer companyid,
			@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<Department> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		map.put("companyid",companyid);
		List<Department> list = departmentService.search(map);
		result.setRows(list);
		result.setTotal(departmentService.count(map));
		return result;
	}
	
	@RequestMapping(value = "/departmentlist",method = RequestMethod.POST)
	@ResponseBody
	public List<Department> departmentlist(@RequestParam(value = "companyid",required = false) Integer companyid){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("companyid",companyid);
		List<Department> list = departmentService.search(map);
		return list;
	}

	@RequestMapping(value = "/departmentbyprojectitem",method = RequestMethod.POST)
	@ResponseBody
	public List<Department> departmentbyprojectitem(@RequestParam(value = "projectitemid",required = false) Integer projectitemid){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Department> list = departmentService.searchByProjectItem(projectitemid);
		return list;
	}
	@SystemControllerLog(description = "修改部门信息")
	@RequestMapping(value = "/departmentSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult departmentSave(@RequestBody Department department){
		OperateResult result = new OperateResult();
		int count = 0;
		if(department.getId() != null && department.getId() > 0){
			count = departmentService.update(department);
		}else{
			department.setStatus(2);
			count = departmentService.insert(department);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "禁用部门信息")
	@RequestMapping(value = "departmentDelete.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult deleteDepartment(@RequestParam(value = "departmentid",required = false) Integer departmentid,
			@RequestParam(value = "status",required = false) Integer status){
		OperateResult result = new OperateResult();
		Map<String,Object> map = new HashMap<>();
		map.put("departmentid",departmentid);
		map.put("status",status);
		int count = departmentService.updateStatus(map);
		result.setSuccess(count > 0);
		return result;
	}
}
